package com.simple.business.util;

import com.simple.business.domain.dto.evaluation.EvaluationQuestionDTO;
import com.simple.business.service.utilService.LLMService;
import com.simple.common.utils.SecurityUtils;
import com.simple.llm.domain.dto.LLMMessageDTO;
import com.simple.llm.domain.vo.LLMResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @desc 大模型工具类
 * @Author Simple
 * @Date 2024/9/26 9:36
 **/
@Component
@Slf4j
public class LLMUtil {
    @Resource
    private LLMService llmService;

    /**
     * @param systemContent        大模型角色
     * @param userInitContent      大模型输入提示
     * @param assistantAndUserMsgs 大模型输入内容
     * @param desc                 大模型调用分类
     * @param returnType           大模型返回结果处理  singleResult:获取json中value，origin:返回json全部内容
     * @return
     */
    public String chatWithMultiMsgs(String systemContent, String userInitContent, List<Map<String, String>> assistantAndUserMsgs, String desc, String returnType) {
        List<LLMMessageDTO> list = new ArrayList<>();
        LLMMessageDTO systemMsg = new LLMMessageDTO("system", systemContent);
        LLMMessageDTO userInit = LLMMessageDTO.userMsg(userInitContent);
        list.add(systemMsg);
        list.add(userInit);
        for (Map<String, String> assistantAndUserMsg : assistantAndUserMsgs) {
            String assistantMsg = assistantAndUserMsg.get("assistantMsg");
            String userMsg = assistantAndUserMsg.get("userMsg");
            list.add(LLMMessageDTO.assistantMsg(assistantMsg));
            list.add(LLMMessageDTO.userMsg(userMsg));
        }
        LLMResponseVO llmResponseVO = Optional.of(llmService.chat(list, desc, SecurityUtils.getUserId())).orElse(new LLMResponseVO());
        String origin = llmResponseVO.getContent();
        return "origin".equals(returnType) ? RuleHandleUtil.dealLLMJsonResult(origin) : RuleHandleUtil.dealSingleJsonResult(origin);
    }

    /**
     * 简化chatWithMultiMsgs的封装map
     * @param obj
     * @return
     */
    public List<Map<String, String>> fillChatWithMultiMsgsMap(Object... obj) {
        List<Map<String, String>> param = new ArrayList<>();
        for (int i = 0; i < obj.length; i += 2) {
            Map<String, String> map = new HashMap<>();
            map.put("assistantMsg", "好的，请输入" + obj[i]);
            map.put("userMsg", obj[i] + ": ```" + obj[i + 1] + "```。");
            param.add(map);
        }
        return param;
    }

    /**
     * 默认返回完整json
     * @return
     */
    public String chatWithSingleMsgAndReturnOrigin(String systemContent, String userInitContent, String assistantMsg, String userMsg, String desc) {
        return chatWithSingleMsg(systemContent, userInitContent, assistantMsg, userMsg, desc, "origin");
    }

    /**
     * @param systemContent
     * @param userInitContent
     * @param assistantMsg
     * @param userMsg
     * @param desc
     * @param returnType
     * @return
     */
    public String chatWithSingleMsg(String systemContent,
                                    String userInitContent,
                                    String assistantMsg,
                                    String userMsg, String desc, String returnType) {
        List<LLMMessageDTO> list = new ArrayList<>();
        LLMMessageDTO systemMsg = new LLMMessageDTO("system", systemContent);
        LLMMessageDTO userInit = LLMMessageDTO.userMsg(userInitContent);
        list.add(systemMsg);
        list.add(userInit);
        list.add(LLMMessageDTO.assistantMsg(assistantMsg));
        list.add(LLMMessageDTO.userMsg(userMsg));
        LLMResponseVO llmResponseVO = llmService.chat(list, desc, SecurityUtils.getUserId());
        String origin = llmResponseVO.getContent();
        log.info(desc + "调用大模型返回结果: " + origin);
        return "origin".equals(returnType) ? RuleHandleUtil.dealLLMJsonResult(origin) : RuleHandleUtil.dealSingleJsonResult(origin);
    }

    /**
     * @return
     */
    public String chatWithLlmName(String systemContent, String userMsg, List<EvaluationQuestionDTO> evaluationQuestionDTOs, String llmName) {

        List<LLMMessageDTO> llmList = new ArrayList<>();
        LLMMessageDTO systemMsg = new LLMMessageDTO("system", systemContent);
        LLMMessageDTO userInit = LLMMessageDTO.userMsg(userMsg);
        llmList.add(systemMsg);
        llmList.add(userInit);

        for (EvaluationQuestionDTO evaluationQuestionDTO : evaluationQuestionDTOs) {
            llmList.add(LLMMessageDTO.assistantMsg("好的，请开始输入问题。"));
            llmList.add(LLMMessageDTO.userMsg("问题：'''" + evaluationQuestionDTO.getQuestion() + "。'''"));
            llmList.add(LLMMessageDTO.assistantMsg("好的，请开始输入评价标准。"));
            llmList.add(LLMMessageDTO.userMsg("评价标准：'''" + evaluationQuestionDTO.getAnswer() + "'''"));
            llmList.add(LLMMessageDTO.assistantMsg("好的，请开始输入回答。"));
            llmList.add(LLMMessageDTO.userMsg("回答：'''" + evaluationQuestionDTO.getUserAnswerAsrVoice() + "。'''"));
        }
        LLMResponseVO llmResponseVO = llmService.chat(llmList, "AI面试报告", SecurityUtils.getUserId(), llmName);
        String origin = llmResponseVO.getContent();
        return RuleHandleUtil.dealLLMJsonResult(origin);
    }


}
